/*
    Copyright (C) 2011  Hugo Arregui, FuDePAN

    This file is part of the FuDePAN Extensible Cache

    Feca is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Feca is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Feca.  If not, see <http://www.gnu.org/licenses/>.
*/

template<class E>
class VectorLoader
{
public:
    typedef VectorLoader<E> Loader;
    typedef unsigned int Key;
    typedef E CacheableObject;

    VectorLoader(std::vector<E>& _container) :
        container(_container),
        k(0)
    {};
    static Key next_key(Key k = -1)
    {
        return k + 1;
    }
    bool load_next(Key& i, CacheableObject& o)
    {
        i = k;
        ++k;
        return load(i, o);
    }
    bool load(Key id, CacheableObject& o)
    {
        bool end = id > (container.size() - 1);
        if (!end)
        {
            o = container[id];
        }
        return end;
    }
    virtual E createEmptyObject(unsigned int k) = 0;
private:
    std::vector<CacheableObject> container;
    Key k;
};

template<class E>
class DefaultVectorLoader : public VectorLoader<E>
{
public:
    typedef DefaultVectorLoader Loader;
    typedef unsigned int Key;
    typedef E CacheableObject;
    DefaultVectorLoader(std::vector<E>& _container) :
        VectorLoader<E>(_container)
    {};
    virtual E createEmptyObject(unsigned int /*k*/)
    {
        return E();
    }
};
